﻿<div class="layui-table-tool" style="min-height:unset; padding-left:0; padding-right:0;margin-bottom:-10px">
    <div class="layui-btn-container" style="padding-left:10px;">
        <a class="layui-btn layui-btn-sm" id="add">新增模块</a>
        <a id="expand" class="layui-btn layui-btn-sm">全部展开</a>
        <a id="fold" class="layui-btn layui-btn-sm">全部收起</a>
    </div>
</div>
<table id="table"></table>
@section Scripts{
<script type="text/html" id="rowbar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="add">新增下级</a>
    <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<script>
    layui.extend({
        treeTable: "/lib/treeTable",
    });
    layui.use(['treeTable'], function() {
        var $ = layui.jquery;
        var treeTable = layui.treeTable;
        // 渲染表格
        var treeTb = treeTable.render({
            elem: '#table',
            even: true,
            tree: {
                iconIndex: 0, // 折叠图标显示在第几列
                idName: 'Id', // 自定义id字段的名称
                pidName: 'ParentId', // 自定义标识是否还有子节点的字段名称
                isPidData: true // 是否是pid形式数据
            },
            url: '/SysResource/Index',
            method: 'post',
            height: 'full-35',
            cols: [
            {
                field: 'ResourceName',
                minWidth: 200,
                title: '模块名称'
            },
            {
                field: 'Id',
                title: '模块编号'
            }, 
            {
                field: 'ResourceUrl',
                title: '模块链接'
            }, 
            {
                field: 'ResourceIcon',
                title: '模块图标'
            }, 
            {
                field: 'SortNumber',
                width: 80,
                align: 'center',
                title: '排序号'
            }, 
            {
                field: 'ResourceType',
                width: 100,
                align: 'center',
                templet: function(d) {
                    if (d.ResourceType == 1) {
                        return '菜单';
                    } else if (d.ResourceType == 2) {
                        return '按钮';
                    }
                },
                title: '模块类型'
            }, 
            {
                field: 'ResourceState',
                width: 100,
                align: 'center',
                templet: function(d) {
                    if (d.ResourceState == 0) {
                        return '<font color="red">禁用</font>';
                    } else if (d.ResourceState == 1) {
                        return '启用';
                    }
                },
                title: '模块状态'
            },
            {
                templet: '#rowbar',
                width: 200,
                align: 'center',
                title: '操作'
            }],
            parseData: function(res) {
                return {
                    "code": res.Code == 200 ? 0 : res.Code, //解析接口状态
                    "msg": res.Message, //解析提示文本
                    "data": res.Data,
                    "count":res.Count
                };
            },
            done: function() {
                treeTb.expandAll();
            }
        });

        $("#add").click(function() {
            add(0);
        })
        $("#expand").click(function() {
            treeTb.expandAll();
        })
        $("#fold").click(function() {
            treeTb.foldAll();
        })

        //监听工具条
        treeTable.on('tool(table)', function(obj) {
            var data = obj.data;
            switch (obj.event) {
                case 'add':
                    add(data.Id);
                    break;
                case 'del':
                    layer.confirm('确定删除该条数据吗？', function(index) {
                        $.ajax({
                            url: '/SysResource/Delete' + '/' + data.Id,
                            success: function(res) {
                                if (res.Code == 200) {
                                    layer.msg('删除成功');
                                    treeTb.refresh();
                                } else {
                                    layer.msg(res.message);
                                }
                            }
                        });
                    });
                    break;
                case 'edit':
                    layer.open({
                        type: 2,
                        title: '编辑模块',
                        area: ['360px', '465px'],
                        content: "/SysResource/Edit/" + data.Id,
                        end: function() {
                            treeTb.refresh();
                        },
                    });
                    break;
            }
        });

        function add(parentId) {
            var index = layer.open({
                title: '新增模块',
                type: 2,
                area: ['400px', '465px'],
                content: "/SysResource/Add/" + parentId,
                end: function() {
                    treeTb.refresh();
                }
            });
        }
    });
</script>
}
